<template>
  <div>
    <h1>歌曲列表</h1>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>歌曲管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card class="box-card">
      <el-row :gutter="20">
        <el-col :span="7">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @change="getSongList">
            <el-button slot="append" icon="el-icon-search" @click="getSongList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="dialogVisible = true">添加歌曲</el-button>
        </el-col>
      </el-row>
      <el-table :data="songList" border stripe>
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column label="title" prop="title"></el-table-column>
        <el-table-column label="author" prop="author"></el-table-column>
        <el-table-column label="style" prop="style"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-tooltip effect="dark" content="编辑用户" placement="top" :enterable="false">
              <el-button type="primary" icon="el-icon-edit" @click="openUpdateSong(scope.row)"></el-button>
            </el-tooltip>
            <el-tooltip effect="dark" content="删除用户" placement="top" :enterable="false">
              <el-button type="danger" icon="el-icon-delete" v-on:click="openDeleteSong(scope.row)"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNumber"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
    <!-- 添加用户对话框 -->
    <el-dialog title="添加用户" :visible.sync="dialogVisible" width="50%" @close="addDialogClosed">
      <!-- 内容主题区域 -->
      <el-form :model="addForm" ref="addFormRef" label-width="70px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="addForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="addForm.password"></el-input>
        </el-form-item>
      </el-form>

      <!-- 底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 修改用户对话框 -->
    <el-dialog
      title="修改歌曲"
      :visible.sync="updateSongDialogVisible"
      width="50%"
      @close="updateDialogClosed"
    >
      <!-- 内容主题区域    :rules="addFormRules"      :model="updateUserData" -->
      <el-form ref="updateSongFormRef" label-width="70px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="updateSongData.title"></el-input>
        </el-form-item>
        <el-form-item label="作者" prop="author">
          <el-input v-model="updateSongData.author"></el-input>
        </el-form-item>
        <el-form-item label="风格" prop="style">
          <el-input v-model="updateSongData.style"></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="updateSongDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateSong">修 改</el-button>
      </span>
    </el-dialog>

    <!-- 删除用户对话框 -->
    <el-dialog title="删除歌曲" :visible.sync="deleteSongDialogVisible" width="30%">
      <!-- 内容主题区域 -->
      <h2>你确定要删除歌曲</h2>
      <el-form :model="deleteSongData" ref="updateSongFormRef" label-width="70px">
        <el-form-item label="SongId" prop="songId">
          <el-input v-model="deleteSongData.songId"></el-input>
        </el-form-item>
      </el-form>

      <!-- 底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="deleteSongDialogVisible = false">取 消</el-button>
        <el-button type="danger" @click="deleteSong">删 除</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryInfo: {
        query: "",
        pageNumber: 1,
        pageSize: 5
      },
      songList: [],
      total: 0,
      dialogVisible: false,
      updateSongDialogVisible: false,
      deleteSongDialogVisible: false,
      addForm: {
        title: "",
        author: "",
        style: ""
      },
      deleteSongData: [],
      updateSongData: []
    };
  },
  created() {
    this.getSongList();
  },
  methods: {
    getSongList() {
      this.$http({
        method: "get",
        params: {
          title: this.queryInfo.query
        },
        url: "song/" + this.queryInfo.pageSize + "/" + this.queryInfo.pageNumber
      })
        .then(response => {
          console.log(response.data.data);
          this.$message.info("获取用户列表成功");
          this.songList = response.data.data.items;
          this.total = response.data.data.total;
        })
        .catch(error => {
          console.log(error);
        });
    },
    handleSizeChange(newSize) {
      console.log(newSize);
      this.queryInfo.pageSize = newSize;
      this.getSongList();
    },
    handleCurrentChange(newPage) {
      console.log(newPage);
      this.queryInfo.pageNumber = newPage;
      this.getSongList();
    },
    addDialogClosed() {
      this.$refs.addFormRef.resetFields();
    },
    updateDialogClosed() {
      this.$refs.updateSongFormRef.resetFields();
    },
    openDeleteSong(rowInfo) {
      console.log(rowInfo.songId);
      this.deleteSongData = rowInfo;
      this.deleteSongDialogVisible = true;
    },
    openUpdateSong(rowInfo) {
      console.log(rowInfo);
      this.updateSongData = rowInfo;
      this.updateSongDialogVisible = true;
    },
    updateSong() {
      this.updateSongDialogVisible = false;
      console.log(this.updateSongData);
      this.$http({
        method: "put",
        url: "song/",
        data: this.updateSongData
      });
    },
    deleteSong() {
      this.deleteSongDialogVisible = false;
      this.$http
        .delete("song/" + this.deleteSongData.songId)
        .then(response => {
          console.log(response);
          this.$message.success("删除成功");
        })
        .catch(error => {
          console.log(error);
        });
    }
  }
};
</script>

<style scoped></style>
